package com.hwadee.property.controller.people;

import com.hwadee.property.pojo.Administrator;
import com.hwadee.property.service.IAdministratorService;
import com.hwadee.property.util.AjaxResult;
import com.hwadee.property.util.JwtUtil;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 管理员Controller类
 */
@RestController
@RequestMapping("/api/administrators")
@Api(tags = "管理员-AdministratorController", description = "管理员相关操作的API接口")
@CrossOrigin
public class AdministratorController {

    @Autowired
    private IAdministratorService administratorService;

    @Autowired
    private JwtUtil jwtUtil;

    @PostMapping("/login")
    @ApiOperation(value = "【管理员】登录", notes = "用于管理员登录，登录成功在data中返回当前管理员id")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "account", value = "用户名", required = true, dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "password", value = "密码", required = true, dataType = "String", paramType = "query"),
    })
    public AjaxResult login(
            @RequestParam(value = "account") String account,
            @RequestParam(value = "password") String password) {
        Administrator administrator = new Administrator();
        administrator.setAdminName(account);
        administrator.setAdminPassword(password);
        Administrator bean = administratorService.doLogin(administrator);
        // System.out.println(bean.getAdminName() + bean.getAdminId());
        if (bean != null) {
            String token = jwtUtil.generateToken(bean.getAdminId(), 0);
            AjaxResult result = AjaxResult.success(bean.getAdminId());
            result.put("token", token);
            return result;
        }
        return AjaxResult.fail();
    }

    /**
     * 查询所有管理员信息
     */
    @GetMapping
    @ApiOperation(value = "【管理员】查询所有管理员信息", notes = "返回所有管理员的列表")
    public AjaxResult getAllAdministrators(@RequestHeader("Authorization") String token) {
        List<Administrator> administrators = administratorService.getAllAdministrators();
        return AjaxResult.success(administrators);
    }

    /**
     * 根据管理员名称查询管理员信息
     */
    @GetMapping("/name/{adminName}")
    @ApiOperation(value = "【管理员】根据管理员名称查询管理员信息", notes = "根据管理员名称返回管理员信息")
    @ApiImplicitParam(name = "adminName", value = "管理员名称", required = true, dataType = "string", paramType = "path")
    public AjaxResult getAdminByName(@RequestHeader("Authorization") String token, @PathVariable String adminName) {
        Administrator admin = administratorService.getAdminByName(adminName);
        return admin != null ? AjaxResult.success(admin) : AjaxResult.fail("管理员未找到");
    }

    /**
     * 更新管理员密码
     */
    @PutMapping("/{adminId}/password")
    @ApiOperation(value = "【管理员】更新管理员密码", notes = "根据管理员ID更新密码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "adminId", value = "管理员ID", required = true, dataType = "long", paramType = "path"),
            @ApiImplicitParam(name = "newPassword", value = "新密码", required = true, dataType = "string", paramType = "query")
    })
    public AjaxResult updatePassword(@RequestHeader("Authorization") String token,
                                     @PathVariable Long adminId,
                                     @RequestParam String newPassword) {
        boolean result = administratorService.updatePassword(adminId, newPassword);
        return result ? AjaxResult.success("密码更新成功") : AjaxResult.fail("密码更新失败");
    }

    /**
     * 添加管理员
     */
    @PostMapping
    @ApiOperation(value = "【管理员】添加管理员", notes = "添加新的管理员")
    public AjaxResult addAdministrator(@RequestHeader("Authorization") String token,
                                       @RequestBody Administrator administrator) {
        boolean result = administratorService.save(administrator);
        return result ? AjaxResult.success("管理员添加成功") : AjaxResult.fail("管理员添加失败");
    }
}
